package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.model.ProductFormatDataModel;
import com.yikaiye.android.yikaiye.model.ProductFormatModel;
import com.yikaiye.android.yikaiye.widget.flowLayout.FlowLayoutManager;
import com.yikaiye.android.yikaiye.widget.flowLayout.SpaceItemDecoration;

import java.util.List;

public class ProductFormatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<ProductFormatDataModel> data;
    private final Context context;

    public ProductFormatAdapter(Context context, List<ProductFormatDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ProductHolder(View.inflate(context, R.layout.item_product_format_layout, null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ProductHolder productHolder = (ProductHolder) holder;
        ProductFormatDataModel classify = data.get(position);

        final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        productHolder.title.setText(classify.getName());
        if (productHolder.itemView.getTag() == null) {
            productHolder.recyclerView.addItemDecoration(new SpaceItemDecoration(dp2px(7)));
            productHolder.itemView.setTag("item");
        }
        productHolder.recyclerView.setLayoutManager(flowLayoutManager);
        productHolder.recyclerView.setAdapter(new FlowAdapter(classify.getData()));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ProductHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private RecyclerView recyclerView;

        public ProductHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            recyclerView = itemView.findViewById(R.id.recyclerView);
        }
    }

    class FlowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<ProductFormatModel> list;
        private ProductFormatModel selectDes;

        public FlowAdapter(List<ProductFormatModel> list) {
            this.list = list;
            if (list.size() > 0) {
                selectDes = list.get(0);
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(context, R.layout.flow_item, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            TextView textView = ((MyHolder) holder).text;

            final ProductFormatModel model = list.get(position);
            textView.setBackgroundResource(model.isSelect() ? R.drawable.shape_1af93f50_2dp : R.drawable.shape_f7f7f7_2dp);
            textView.setTextColor(model.isSelect() ? 0xFFF93F50 : 0xFF3B3A40);
            textView.setText(model.getName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (model != selectDes) {
                        if (selectDes != null) {
                            selectDes.setSelect(false);
                        }
                    }
                    model.setSelect(true);
                    selectDes = model;
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyHolder extends RecyclerView.ViewHolder {

            private TextView text;

            public MyHolder(View itemView) {
                super(itemView);
                text = itemView.findViewById(R.id.flow_text);
            }
        }
    }

    private int dp2px(float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }
}
